package com.cn.xarm.client.controller;

import com.cn.common.controller.BaseController;
import com.cn.xarm.client.domain.Score;
import com.cn.xarm.client.domain.Student;
import com.cn.xarm.client.service.ScoreModelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping("/client")
public class ScoreModelController extends BaseController {
    Logger logger = LoggerFactory.getLogger(ScoreModelController.class);
    @Autowired
    private ScoreModelService scoreModelService;
    private final static int TOTAL_PAGE_HALF = 32;

    /**
     * 中文成绩单
     */
    @RequestMapping("/print/data")
    public String test(Model model) {
        String num = "90108122";
        Student student = scoreModelService.get(num);
        logger.info(student.getName() + "=============");
        model.addAttribute("student", student);
        List<Score> scoreList = student.getScores();
        List<Score> scoreList1 = new LinkedList<>();
        for (int i = 0; i < scoreList.size(); i++) {
            Score score = scoreList.get(i);
            if (score.getName().length() > 6) {
                Score s = new Score();
                s.setName(score.getName().substring(6));
                score.setName(score.getName().substring(0, 6));
                scoreList1.add(score);
                scoreList1.add(s);
            } else {
                scoreList1.add(score);
            }
        }
        int totalPage = scoreList1.size() / (TOTAL_PAGE_HALF * 2) + 1;
        model.addAttribute("scores", scoreList1);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("page_half", TOTAL_PAGE_HALF);
        return "client/printData/score";
    }

    public static void main(String[] args) {
        String str = "中医护理学含针灸";
        System.out.println(str.substring(0, 6));
    }


    @RequestMapping("/students")
    public String list(Model model) {
        return "client/printData/student";
    }
}
